﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL.Project;
using BLL.ProjectArtist;
using Newtonsoft.Json;

namespace WebApplication.Project
{
    public partial class Handle : BaseHandle
    {
        private ProjectService projectService = new ProjectService();
        private ProjectArtistService projectArtistService = new ProjectArtistService();
        public void Insert()
        {
            Utils.Model<Models.Projects> modelUtils = new Utils.Model<Models.Projects>();
            Models.Projects project = modelUtils.GetModel(Request.Form);
            int uid = Convert.ToInt32(Session["UID"]);

            project.OwnerID = uid;

            projectService.SaveProject(project);

            if (project.ProjectID != null)
            {
                Response.Write(JsonConvert.SerializeObject(new { code = 0, data = new { id = project.ProjectID }, msg = "需求发布成功" }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "需求发布失败" }));
            }
        }

        public void Update()
        {
            Utils.Model<Models.Projects> modelUtils = new Utils.Model<Models.Projects>();
            Models.Projects project = modelUtils.GetModel(Request.Form);
            Models.Projects queryProject = projectService.GetProjectById(project.ProjectID);

            int uid = Convert.ToInt32(Session["UID"]);
            Models.User user = ((Models.User)Session["USER"]);

            if (queryProject.OwnerID != uid)
            {
                if(user.Role != 2)
                {
                    Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "您无权更新该需求!" }));
                    return;
                }
            }

            project.OwnerID = queryProject.OwnerID;
            

            if (projectService.UpdateProject(project))
            {
                Response.Write(JsonConvert.SerializeObject(new { code = 0, data = new { id = project.ProjectID }, msg = "需求编辑成功" }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "需求编辑失败" }));
            }
        }

        public void Delete()
        {

            int id = Convert.ToInt32(Request.Form["id"]);
            Models.Projects queryProject = projectService.GetProjectById(id);
            int uid = Convert.ToInt32(Session["UID"]);

            if (queryProject.OwnerID != uid)
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "您无权删除该作品!" }));
                return;
            }


            if (projectService.DeleteProjectById(id))
            {
                Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "作品删除成功" }));
            }
            else
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "作品删除失败" }));
            }
        }

        public void Apply()
        {
            int id = Convert.ToInt32(Request.Form["id"]);
            int uid = Convert.ToInt32(Session["UID"]);

            if (projectArtistService.Apply(id, uid)) {
                Response.Write(JsonConvert.SerializeObject(new { code = 0, msg = "需求应征操作成功" }));
            } else
            {
                Response.Write(JsonConvert.SerializeObject(new { code = -1, msg = "需求应征操作失败" }));
            }
        }
    }
}